import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "@/store/userStore";
import { updateFavicon } from "@/comment/method";
import IndexView from "../view/indexView/indexView.vue";
import VolunteerLogin from "../view/loginView/volunteerLogin.vue";
import MemberLogin from "../view/loginView/memberLogin.vue";
import volunteer from "../view/volunteer/indexView.vue";
import AccountView from "../view/volunteer/accountView.vue";
import CustomView from "../view/volunteer/customView.vue";
import TabulationView from "../view/volunteer/tabulationView.vue";
import ScheduleView from "../view/volunteer/scheduleView.vue";
import ProgrammeView from "../view/volunteer/programmeView.vue";
import EnrollView from "../view/volunteer/enrollView.vue";
const VOLUNTEER = [
  {
    path: "account",
    name: "account",
    component: AccountView,
    meta: {},
  },
  {
    path: "custom",
    name: "custom",
    component: CustomView,
    meta: {},
  },
  {
    path: "tabulation",
    name: "tabulation",
    component: TabulationView,
    meta: {},
  },
  {
    path: "schedule",
    name: "schedule",
    component: ScheduleView,
    meta: {},
  },
  {
    path: "programme",
    name: "programme",
    component: ProgrammeView,
    meta: {},
  },
  {
    path: "enroll",
    name: "enroll",
    component: EnrollView,
    meta: {},
  },
];
const routes = [
  {
    path: "/",
    name: "index",
    component: IndexView,
    meta: {},
  },
  {
    path: "/memberLogin",
    name: "memberLogin",
    component: MemberLogin,
    meta: {},
  },
  {
    path: "/volunteerLogin",
    name: "volunteerLogin",
    component: VolunteerLogin,
    meta: {},
  },
  {
    path: "/volunteer",
    name: "volunteer",
    component: volunteer,
    children: VOLUNTEER,
    meta: {},
  },
];
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes,
});
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  // document.title = to.meta.title || "My Vue App";
  // updateFavicon("/位置.svg");
  const pathList = ["/", "/memberLogin", "/volunteerLogin"];
  if (!pathList.includes(to.path)) {
    if (!userStore.isLogin) {
      if (to.path.includes("/volunteer")) {
        return next("/volunteerLogin");
      }
      if (to.path.includes("/member")) {
        return next("/memberLogin");
      }
    } else {
      userStore.lastPath = to.path;
      next();
    }
  } else {
    next();
  }
});

export default router;
